package com.lzy.product.apis.impl;

import com.github.pagehelper.PageInfo;
import com.lzy.base.utils.PasswordUtil;
import com.lzy.base.utils.UserUtil;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.utils.MD5Utils;
import com.lzy.framework.core.utils.StringUtil;
import com.lzy.framework.tools.security.Digests;
import com.lzy.framework.tools.security.Encodes;
import com.lzy.product.apis.ProductUserServiceApi;
import com.lzy.product.dtos.ProductUserDto;
import com.lzy.product.service.ProductUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by WIN7 on 2017/11/28.
 */
@Service
public class ProductUserServiceApiImpl implements ProductUserServiceApi {

    @Autowired
    ProductUserService productUserService;

    @Override
    public Long insert(ProductUserDto dto) {
        // 初始化用户信息
        dto.setPassword(MD5Utils.MD5(PasswordUtil.defaultPass));
        /*if (StringUtil.isEmpty(dto.getUserId().toString())) {
            dto.setUserId(UserUtil.getCurrentUser().getUserId());
        }*/
        entryptPassword(dto);
        return productUserService.insert(dto);
    }

    /**
     * 设定安全的密码，生成随机的salt并经过1024次 sha-1 hash
     */
    private void entryptPassword(ProductUserDto user) {
        byte[] salt = Digests.generateSalt(Integer.valueOf(PasswordUtil.saltSize));
        user.setSalt(Encodes.encodeHex(salt));

        byte[] hashPassword = Digests.md5(user.getPassword().getBytes(), salt,
                Integer.valueOf(PasswordUtil.hashIntegrations));
        user.setPassword(Encodes.encodeHex(hashPassword));
    }

    @Override
    public Boolean update(ProductUserDto dto) {
        return productUserService.update(dto);
    }

    @Override
    public Boolean delete(Long id) {
        return productUserService.delete(id);
    }

    @Override
    public Boolean remove(Long id) {
        return productUserService.remove(id);
    }

    @Override
    public ProductUserDto findById(Long id) {
        return productUserService.findById(id);
    }

    @Override
    public List<ProductUserDto> query(ProductUserDto dto) {
        return productUserService.query(dto);
    }

    @Override
    public ProductUserDto findUserByCont(ProductUserDto userDto) {
        return productUserService.findUserByCont(userDto);
    }

    @Override
    public PageInfo<ProductUserDto> queryUserList(Pager pager, ProductUserDto user) {
        return productUserService.queryUserList(pager, user);
    }

    @Override
    public List<ProductUserDto> selectUserListByUserId(Long userId) {
        return productUserService.selectUserListByUserId(userId);
    }

    @Override
    public String maxiNumber(String expression) {
        return productUserService.maxiNumber(expression);
    }

    @Override
    public PageInfo<ProductUserDto> queryAgents(Pager pager, ProductUserDto user) {
        return productUserService.queryAgents(pager, user);
    }

    @Override
    public List<Integer> queryAllCitycode() {
        return productUserService.queryAllCitycode();
    }

    @Override
    public ProductUserDto queryByPhone(String phone) {
        ProductUserDto productUserDto = new ProductUserDto();
        productUserDto.setPhone(phone);
        List<ProductUserDto> productUserDtoList = productUserService.query(productUserDto);
        if (null != productUserDtoList && productUserDtoList.size() == 1) {
            return productUserDtoList.get(0);
        }
        return null;
    }

    @Override
    public List<ProductUserDto> selectListById(Long id, Integer type) {
        return productUserService.selectListById(id, type);
    }

    @Override
    public ProductUserDto queryAgentByphone(String phone) {
        return productUserService.queryAgentByphone(phone);
    }


}
